package com.yc.supermarket.web.controller;

import com.yc.supermarket.entity.*;
import com.yc.supermarket.mapper.GoodsInfoMapper;
import com.yc.supermarket.mapper.ShoppingInfoMapper;
import com.yc.supermarket.service.GoodsInfoService;
import com.yc.supermarket.service.impl.GoodsInfoServiceImpl;
import com.yc.supermarket.util.MybatisUtil;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/users")
public class GoodsInfoController extends BaseController {
    private GoodsInfoService service = new GoodsInfoServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String op = req.getParameter("op");
        if ("Goodsfind".equals(op)) {
            doGoodsFind(req, resp);
        } else if ("Productsfind".equals(op)) {
            doProductsFind(req, resp);
        }else if ("Goodsf".equals(op)) {
            doGoodsF(req, resp);
        }else if ("Goodslist".equals(op)) {
            doGoodsList(req, resp);
        }else if ("Goodsfindbyid".equals(op)) {
            doGoodsFindByid(req, resp);
        }/*else if ("addShoppingCard".equals(op)) {
            doAddShoppingCard(req, resp);
        }*/else if ("UsersInfoByName".equals(op)) {
            doUsersInfoByName(req, resp);
        }else if ("getMember".equals(op)) {
            doGetMember(req, resp);
        }else if ("MemberById".equals(op)) {
            doMemberById(req, resp);
        }else if ("showMemberInfo".equals(op)) {
            doshowMemberInfo(req, resp);
        }
        else if ("addBalance".equals(op)) {
            doAddBalance(req, resp);
        }/*else if ("queryQuantity".equals(op)) {
            doAddShoppingCard1(req, resp);
        }else if ("updateShoppingCard".equals(op)) {
            doAddShoppingCard2(req, resp);
        }else if ("addShoppingCard".equals(op)) {
            doAddShoppingCard3(req, resp);
        }*/else if ("ShoppingCard".equals(op)) {
            doAddShoppingCard4(req, resp);
        }else if ("ShoppingCardQ".equals(op)) {
            doAddShoppingCard5(req, resp);
        }else if ("Collect".equals(op)) {
            doAddCollect(req, resp);
        }else if ("CollectGoodsf".equals(op)) {
            doGetCollectById(req, resp);
        }else if ("delCollect".equals(op)) {
            doDelCollect(req, resp);
        }
    }

    protected void doGoodsFind(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        GoodsInfoService service = new GoodsInfoServiceImpl();
        Commodity goods=parseRequest(req,Commodity.class);
        System.out.println(goods);
        Map<String,Object> commodities = service.getCommoditiesByTypeId(goods.getTypeId());
        System.out.println(commodities);
        printToJson(resp,commodities);
    }

    protected void doProductsFind(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        GoodsInfoService service = new GoodsInfoServiceImpl();
        Map<String,Object> productTypes = service.getProductType();
        printToJson(resp,productTypes);
    }
    protected void doGoodsF(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        GoodsInfoService service = new GoodsInfoServiceImpl();
        Map<String,Object> comodities = service.getCommodities();
        printToJson(resp,comodities);
    }
    protected void doGoodsList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        GoodsInfoService service = new GoodsInfoServiceImpl();
        Map<String,Object> comoditiesList = service.getCommodityList();
        printToJson(resp,comoditiesList);
    }
    protected void doGoodsFindByid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        GoodsInfoService service = new GoodsInfoServiceImpl();
        Commodity goods=parseRequest(req,Commodity.class);
        System.out.println(goods);
        Map<String,Object> commodities = service.getCommoditiesByComId(goods.getCommodityId());
        System.out.println(commodities);
        printToJson(resp,commodities);
    }
/*    protected void doAddShoppingCard(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ShoppingInfoMapper mapper;
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(ShoppingInfoMapper.class);
        ShoppingCart shoppingCart=parseRequest(req,ShoppingCart.class);
         String userId=req.getParameter("userId");
        String productId=req.getParameter("productId");
        int a= mapper.addToCart(shoppingCart);
        if(a>0){
          *//*  System.out.println(a+"___________");*//*
            printToJson(resp,a);
        }else {
            printToJson(resp,0);
        }

    }*/
/*protected void doAddShoppingCard1(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ShoppingInfoMapper mapper;
    SqlSession session = MybatisUtil.getSession();
    mapper = session.getMapper(ShoppingInfoMapper.class);
    ShoppingCart shoppingCart=parseRequest(req,ShoppingCart.class);
    String userId=req.getParameter("userId");
    String productId=req.getParameter("productId");
    ShoppingCart shoppingCart1 = mapper.queryShoppingCart(Integer.parseInt(userId), Integer.parseInt(productId));
    System.out.println(shoppingCart1+"___________");
    if (userId == null || productId == null) {
        printToJson(resp,0);
    }else {
        printToJson(resp, 1);
    }
}
    protected void doAddShoppingCard2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ShoppingInfoMapper mapper;
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(ShoppingInfoMapper.class);
        ShoppingCart shoppingCart=parseRequest(req,ShoppingCart.class);
        String userId=req.getParameter("userId");
        String productId=req.getParameter("productId");
        int a= mapper.insertShoppingCart(shoppingCart);
        if(a>0){
            *//*  System.out.println(a+"___________");*//*
            printToJson(resp,a);
        }else {
            printToJson(resp,0);
        }

    }
    protected void doAddShoppingCard3(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ShoppingInfoMapper mapper;
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(ShoppingInfoMapper.class);
        ShoppingCart shoppingCart=parseRequest(req,ShoppingCart.class);
        String userId=req.getParameter("userId");
        String productId=req.getParameter("productId");
        int a= mapper.updateShoppingCart(shoppingCart);
        if(a>0){
            *//*  System.out.println(a+"___________");*//*
            printToJson(resp,a);
        }else {
            printToJson(resp,0);
        }

    }*/
    protected void doAddShoppingCard4(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ShoppingInfoMapper mapper;
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(ShoppingInfoMapper.class);
        ShoppingCart shoppingCart=parseRequest(req,ShoppingCart.class);
        String userId=req.getParameter("userId");
        String productId=req.getParameter("productId");
        ShoppingCart shoppingCart1 = mapper.queryShoppingCart(Integer.parseInt(userId), Integer.parseInt(productId));
        if (shoppingCart1 == null) {
            int b= mapper.insertShoppingCart(shoppingCart);
            if(b>0){
                printToJson(resp,b);
            }else {
                printToJson(resp,0);
            }
        }
        else {
            shoppingCart1.setQuantity(shoppingCart1.getQuantity() + 1);
            int a= mapper.updateShoppingCart(shoppingCart1);
            if(a>0){
                printToJson(resp,a);
            }else {
                printToJson(resp,0);
            }
        }
    }

    protected void doAddShoppingCard5(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ShoppingInfoMapper mapper;
        SqlSession session = MybatisUtil.getSession();
        mapper = session.getMapper(ShoppingInfoMapper.class);
        ShoppingCart shoppingCart=parseRequest(req,ShoppingCart.class);
        String userId=req.getParameter("userId");
        String productId=req.getParameter("productId");
        String quantity=req.getParameter("quantity");
        ShoppingCart shoppingCart1 = mapper.queryShoppingCart(Integer.parseInt(userId), Integer.parseInt(productId));
        if (shoppingCart1 == null) {
            int b= mapper.insertShoppingCart1(shoppingCart);
            if(b>0){
                printToJson(resp,b);
            }else {
                printToJson(resp,0);
            }
        }
        else {
            shoppingCart1.setQuantity(shoppingCart1.getQuantity() + Integer.parseInt(quantity));
            int a= mapper.updateShoppingCart(shoppingCart1);
            if(a>0){
                printToJson(resp,a);
            }else {
                printToJson(resp,0);
            }
        }
    }

    protected void doAddCollect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        GoodsInfoService service=new GoodsInfoServiceImpl();
        Collect collect=parseRequest(req,Collect.class);
        int i = service.addCollect(collect);
        printToJson(resp,i);
    }
    protected void doGetCollectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int userId =Integer.parseInt(req.getParameter("userId"));
        Map<String, Object> collectById = service.getCollectById(userId);
        printToJson(resp,collectById);
    }

    protected void doDelCollect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int userId =Integer.parseInt(req.getParameter("userId"));
        int commodityId =Integer.parseInt(req.getParameter("commodityId"));
        int i = service.delCollect(userId, commodityId);
        printToJson(resp,i);
    }

    protected void doUsersInfoByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        GoodsInfoService service=new GoodsInfoServiceImpl();
        User user=parseRequest(req,User.class);
        Map<String, Object> userByName = service.getUserByName(user.getUserName());
        printToJson(resp,userByName);
    }
    protected void doGetMember(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        GoodsInfoService service=new GoodsInfoServiceImpl();
        User user=parseRequest(req,User.class);
        System.out.println(user+"------------");
        Member member=parseRequest(req,Member.class);
        System.out.println("the:"+member);
        System.out.println("valeuss:"+member.getBalance());
        int a=service.becomeMember(user.getUserName(),member.getBalance(),member.getRegistrationDate());
        printToJson(resp,a);
    }
    protected void doMemberById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        GoodsInfoService service=new GoodsInfoServiceImpl();
        User user=parseRequest(req,User.class);
        int b = service.getUserById(user.getUserName());
        printToJson(resp,b);
    }
    protected void doshowMemberInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        GoodsInfoService service=new GoodsInfoServiceImpl();
        User user=parseRequest(req,User.class);
        Map<String, Object> m = service.getMemberInfoById(user.getUserName());
        printToJson(resp,m);
    }
    protected void doAddBalance(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        GoodsInfoService service=new GoodsInfoServiceImpl();
        User user=parseRequest(req,User.class);
        //System.out.println(user+"------------");
        /*Member member=parseRequest(req,Member.class);*/
        //System.out.println("the:"+member);
        //System.out.println("valeuss:"+member.getBalance());
        int a=service.addBalance(user.getUserName(),user.getBalance());
        printToJson(resp,a);
    }
}

